package com.example.its2021606134chh.BusDiyModule;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IsPayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IsPayFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private String[][] childs = new String[][]{};


    public IsPayFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment IsPayFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static IsPayFragment newInstance(String param1, String param2) {
        IsPayFragment fragment = new IsPayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_is_pay, container, false);
        // Inflate the layout for this fragment
        ExpandableListView expandListIsPay = (ExpandableListView) view.findViewById(R.id.expand_list_isPay);
        List<BusPayData> groups = new ArrayList<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                    @Override
                    public void onFailure(Exception e) {
                    }

                    @Override
                    public void onResponse(String json) {
                        Log.i("TAG", json);
                        Gson gson = new Gson();
                        BusDiy diy = gson.fromJson(json, BusDiy.class);
                        List<BusDiy.ROWSDETAILBean> Temp = diy.getROWS_DETAIL();
                        for (int i = 0; i < Temp.size(); i++) {
                            int flag = Temp.get(i).getFlag();
                            if (flag == 1) {
                                Random random = new Random();
                                String busstop_start = Temp.get(i).getBusstop_start();
                                String busstop_end = Temp.get(i).getBusstop_end();
                                String busstop_date = Temp.get(i).getBusstop_date();
                                String[] dateArr = busstop_date.split(",");
                                String price = String.valueOf(random.nextInt(10) + 1);
                                groups.add(new BusPayData(busstop_start, busstop_end, price));
                                childs = Arrays.copyOf(childs, childs.length + 1);
                                childs[childs.length - 1] = dateArr;
                            }
                        }
                        PayExpandableListviewAdapter adapter = new PayExpandableListviewAdapter(getContext(), groups, childs, 1);
                        expandListIsPay.setAdapter(adapter);
                    }
                }, "get_busline_order", new String[]{"UserName"}, "user1");
            }
        }).start();
//        groups.add(new BusPayData("光谷金融街", "南湖商厦", "8"));
//        groups.add(new BusPayData("光谷金融街", "南湖商厦", "8"));
//        String[][] childs = new String[][]{{"2018-04-03", "2018-04-04", "2018-04-05"}, {"2018-04-06", "2018-04-07", "2018-04-08"}};
//        PayExpandableListviewAdapter adapter = new PayExpandableListviewAdapter(getContext(), groups, childs, 1);
//        expandListIsPay.setAdapter(adapter);
//        expandListIsPay.expandGroup(0);
        return view;
    }
}